﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using sandpit.Client.Enum;

namespace sandpit.Client.Helper
{
    /// <summary>
    /// 海康威视摄像头SDK帮助类
    /// </summary>
    public class HikSDKHelper
    {
        static HikSDKHelper()
        {
            // 设置SDK扫描路径
            Directory.SetCurrentDirectory($"{Application.StartupPath}\\Sdk");
        }

        /// <summary>
        /// 初始化SDK，调用其他SDK函数的前提。
        /// </summary>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_Init")]
        public static extern bool Init();

        /// <summary>
        /// 释放SDK资源，在程序结束前调用。
        /// </summary>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_Cleanup")]
        public static extern bool Cleanup();

        /// <summary>
        /// 返回最后操作的错误码
        /// </summary>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_GetLastError")]
        public static extern uint GetLastError();

        /// <summary>
        /// 获取设备能力集
        /// </summary>
        /// <param name="iUserID">登录凭证</param>
        /// <param name="dwAbilityType">能力类型</param>
        /// <param name="pInBuf">输入缓冲区指针</param>
        /// <param name="dwInLength">输入缓冲区的长度</param>
        /// <param name="pOutBuf">输出缓冲区指针</param>
        /// <param name="dwOutLength">接收数据的缓冲区的长度</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_GetDeviceAbility")]
        public static extern bool GetDeviceAbility(int iUserID, uint dwAbilityType, IntPtr pInBuf, uint dwInLength, IntPtr pOutBuf, uint dwOutLength);

        /// <summary>
        /// 设置SDK本地参数
        /// </summary>
        /// <param name="enumType">配置类型</param>
        /// <param name="ipInBuff">输入参数</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_SetSDKLocalCfg")]
        public static extern bool SetSDKLocalCfg(NET_SDK_LOCAL_CFG_TYPE enumType, IntPtr ipInBuff);

        /// <summary>
        /// 启用日志文件写入接口
        /// </summary>
        /// <param name="nLogLevel">日志的等级</param>
        /// <param name="strLogDir">日志文件的路径</param>
        /// <param name="bAutoDel">是否删除超出的文件数</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_SetLogToFile")]
        public static extern bool SetLogToFile(int nLogLevel, string strLogDir, bool bAutoDel);

        /// <summary>
        /// 启动监听，接收设备主动上传的报警等信息
        /// </summary>
        /// <param name="sLocalIP">PC机本地IP地址</param>
        /// <param name="wLocalPort">PC本地监听端口号</param>
        /// <param name="dataCallback">回调函数</param>
        /// <param name="pUserData">用户数据</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_StartListen_V30")]
        public static extern int StartListen_V30(string sLocalIP, ushort wLocalPort, MSGCallBack dataCallback, IntPtr pUserData);

        /// <summary>
        /// 注册回调函数，接收设备报警消息等
        /// </summary>
        /// <param name="fMessageCallBack">回调函数</param>
        /// <param name="pUser">用户数据</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_SetDVRMessageCallBack_V31")]
        public static extern bool SetDVRMessageCallBack_V31(MSGCallBack_V31 fMessageCallBack, IntPtr pUser);

        /// <summary>
        /// 停止监听
        /// </summary>
        /// <param name="iListenHandle">监听句柄</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_StopListen_V30")]
        public static extern bool StopListen_V30(int iListenHandle);

        /// <summary>
        /// 用户注册设备
        /// </summary>
        /// <param name="sDVRIP">设备IP地址或是静态域名，字符数不大于128个</param>
        /// <param name="wDVRPort">设备端口号</param>
        /// <param name="sUserName">登录的用户名</param>
        /// <param name="sPassword">用户密码</param>
        /// <param name="ipDeviceInfo">设备信息</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_Login_V30")]
        public static extern int Login(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 ipDeviceInfo);

        /// <summary>
        /// 用户注销
        /// </summary>
        /// <param name="iUserID">用户ID号</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_Logout")]
        public static extern bool Logout(int iUserID);

        /// <summary>
        /// 实时预览（支持多码流）
        /// </summary>
        /// <param name="iUserID">登录接口的返回值</param>
        /// <param name="ipPreviewInfo">预览参数</param>
        /// <param name="fRealDataCallBack_V30">码流数据回调函数</param>
        /// <param name="pUser">用户数据</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_RealPlay_V40")]
        public static extern int RealPlay(int iUserID, ref NET_DVR_PREVIEWINFO ipPreviewInfo, REALDATACALLBACK fRealDataCallBack_V30, IntPtr pUser);

        /// <summary>
        /// 停止预览
        /// </summary>
        /// <param name="iRealHandle">预览句柄</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_StopRealPlay")]
        public static extern bool StopRealPlay(int iRealHandle);

        /// <summary>
        /// 建立报警上传通道，获取报警等信息
        /// </summary>
        /// <param name="iUserID">登录接口的返回值</param>
        /// <param name="ipSetupParam">报警布防参数</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_SetupAlarmChan_V41")]
        public static extern int SetupAlarmChan_V41(int iUserID, ref NET_DVR_SETUPALARM_PARAM ipSetupParam);

        /// <summary>
        /// 撤销报警上传通道
        /// </summary>
        /// <param name="iAlarmHandle">监听通道Id</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_CloseAlarmChan_V30")]
        public static extern int CloseAlarmChan_V30(int iAlarmHandle);

        /// <summary>
        /// 独占声卡模式下开启声音
        /// </summary>
        /// <param name="iRealHandle">预览句柄</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_OpenSound")]
        public static extern bool OpenSound(int iRealHandle);

        /// <summary>
        /// 独占声卡模式下关闭声音
        /// </summary>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_CloseSound")]
        public static extern bool CloseSound();

        /// <summary>
        /// 调节播放音量
        /// </summary>
        /// <param name="iRealHandle">预览句柄</param>
        /// <param name="wVolume">音量，取值范围[0, 0xffff]</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_Volume")]
        public static extern bool Volume(int iRealHandle, ushort wVolume);

        /// <summary>
        /// 预览时抓图并保存在指定内存中
        /// </summary>
        /// <param name="iRealHandle">预览句柄</param>
        /// <param name="pPicBuf">保存图片数据的缓冲区</param>
        /// <param name="dwPicSize">缓冲区大小，分配的缓冲区内存必须大于等于图片数据的大小</param>
        /// <param name="pSizeReturned">返回图片数据的实际大小</param>
        /// <returns></returns>
        [DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_CapturePictureBlock_New")]
        public static extern bool CapturePictureBlock(int iRealHandle, IntPtr pPicBuf, uint dwPicSize, ref uint pSizeReturned);
    }
}
